package cn.robot.upload.service.impl;

import cn.robot.upload.service.FileService;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;

/**
 * @version V3.0
 * @Title: FileServiceImpl
 * @Description: 描述
 * @author: Trident
 * @date 2020/10/3 上午9:48
 */
@Service
public class FileServiceImpl implements FileService {

    @Override
    public String upload(String name, String miner, String token) throws Exception {
        // 创建Httpclient对象,相当于打开了浏览器
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://171.214.13.227:8033/api/v1/storage_deal/upload");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        httpPost.addHeader("token", token);
        builder.addBinaryBody("file", new File("/file/" + name));// 文件路径
        builder.addTextBody("miner", miner);//设置请求参数
        httpPost.setEntity(builder.build());
        CloseableHttpResponse response = null;
        try {
            // 执行请求，相当于敲完地址后按下回车。获取响应
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                // 解析响应，获取数据
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                // 关闭资源
                response.close();
            }
            // 关闭浏览器
            httpclient.close();
        }
        return null;
    }

    @Override
    public ArrayList<String> getFileName(String path) {
        ArrayList<String> files = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                files.add(tempList[i].getName());
            }
        }
        return files;
    }

    @Override
    public String createFile(String filePath, String fileName) {
        Path path = Paths.get(filePath + fileName);
        byte[] output = new byte[1];
        ByteBuffer bf = ByteBuffer.wrap(output);
        try (FileChannel fc = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
            fc.position((3L << 30) - 1);
            fc.write(bf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }
}
